package org.example.cglib.cglibProxy;

import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.InvocationTargetException;

/**
 * @author : yuhuan.wu
 * @date : 2021/11/9
 * @description :
 */
public class CGlibProxy {

    public static void main(String[] args) throws InvocationTargetException {

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\cglib2");

        //创建Enhancer对象，类似于JDK动态代理的Proxy类，下一步就是设置几个参数
        Enhancer enhancer = new Enhancer();
        //设置目标类的字节码文件
        enhancer.setSuperclass(RealSubject.class);
        //设置回调函数
        enhancer.setCallback(new MyMethodInterceptor());

        //这里的creat方法就是正式创建代理类
        RealSubject proxyDog = (RealSubject) enhancer.create();
        //调用代理类的eat方法
        proxyDog.request();

        System.out.println("===");
//
//        FastClass fastA = FastClass.create(proxyDog.getClass());
//        fastA.invoke("request", Constants.EMPTY_CLASS_ARRAY, proxyDog, new Object[]{});

    }

}
